package com.xh.cdp.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xh.cdp.common.BaseController;
import com.xh.cdp.sys.entity.SysMenuEntity;
import com.xh.cdp.sys.entity.SysUserEntity;
import com.xh.cdp.sys.entity.temp.ComboTreeModel;
import com.xh.cdp.sys.service.SysMenuService;

@Controller
@RequestMapping("/sys/menu")
public class MenuController extends BaseController {

	private static Logger log = LogManager.getLogger(MenuController.class);

	@Autowired
	private SysMenuService sysMenuService;

	@RequestMapping("/showLeftMenuTree")
	@ResponseBody
	public List<SysMenuEntity> showLeftMenuTree(String id, HttpSession session,
			HttpServletResponse response) throws Exception {
		if (id == null) {
			id = "0";
		}
		SysUserEntity user = (SysUserEntity) session.getAttribute("user");
		return sysMenuService.showLeftMenuTree(id, user.getRoleId());
	}

	/**
	 * 查询所有菜单（包括父节点）
	 * 
	 * @param id
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listMenus")
	@ResponseBody
	public List<SysMenuEntity> listMenus(String id, HttpSession session,
			HttpServletResponse response) throws Exception {
		return sysMenuService.listMenus(id);
	}

	/**
	 * 查询所有父节点
	 * 
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listParentMenus")
	@ResponseBody
	public List<SysMenuEntity> listParentMenus(HttpSession session,
			HttpServletResponse response) throws Exception {
		return sysMenuService.listParentMenus();
	}

	/**
	 * 增加菜单
	 * 
	 * @param sysMenuEntity
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addMenu/{pid}")
	@ResponseBody
	public String addMenu(SysMenuEntity sysMenuEntity,
			@PathVariable String pid, HttpSession session,
			HttpServletResponse response) throws Exception {
		System.out.println("pid:" + pid);
		String result = sysMenuService.addMenu(sysMenuEntity);
		if (result != null) {
			return "success";
		}
		return "";
	}

	/**
	 * 通过ID查询指定菜单的属性
	 * 
	 * @param id
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listMenuById")
	@ResponseBody
	public List<Object> listMenuById(String id, HttpSession session,
			HttpServletResponse response) throws Exception {
		System.out.println("sysMenuService.listMenuById(id):"
				+ sysMenuService.listMenuById(id));
		return sysMenuService.listMenuById(id);
	}

	/**
	 * 通过ID更新菜单
	 * 
	 * @param id
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateMenuById")
	@ResponseBody
	public String updateMenuById(SysMenuEntity sysMenuEntity, String id)
			throws Exception {
		System.out.println("id:" + id);
		sysMenuService.updateMenuById(sysMenuEntity, id);
		return "success";
	}

	/**
	 * 通过ID假删除Menu
	 * 
	 * @param id
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteMenuById")
	@ResponseBody
	public String deleteMenuById(String id, HttpSession session,
			HttpServletResponse response) throws Exception {
		Integer row = sysMenuService.deleteMenuById(id);
		if (row > 0) {
			return "success";
		}
		return "";
	}

	@RequestMapping("/getPowerCombotree")
	@ResponseBody
	public List<ComboTreeModel> getPowerCombotree() {
		return sysMenuService.getPowerCombotree();
	}

	@RequestMapping("/getPowerCombotree/{id}")
	@ResponseBody
	public List<ComboTreeModel> getPowerCombotree(@PathVariable String id) {
		return sysMenuService.getPowerCombotree(id);
	}

}
